package com.zshan.clinic.web.service;

import com.zshan.clinic.admin.service.medical.ClinicMedicalService;
import com.zshan.clinic.admin.service.medical.ClinicRegisterService;
import com.zshan.clinic.admin.service.patient.ClinicPatientService;
import com.zshan.clinic.common.util.decimal.BigDecimalUtils;
import com.zshan.clinic.common.util.string.StringUtil;
import com.zshan.clinic.database.model.ClinicMedical;
import com.zshan.clinic.database.model.ClinicPatient;
import com.zshan.clinic.database.model.ClinicRegister;
import com.zshan.clinic.web.entity.shanghe.ShangHeUploadRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 上禾身高体重测量数据采集
 * @author lichen
 */
@Slf4j
@Service
public class ShangHeService {

    @Autowired
    private ClinicRegisterService clinicRegisterService;

    @Autowired
    private ClinicMedicalService clinicMedicalService;

    @Autowired
    private ClinicPatientService clinicPatientService;


    /**
     * 更新患者的身高体重
     * @param shangHeUploadRequest
     */
    public void upload(ShangHeUploadRequest shangHeUploadRequest) {
        log.info("上禾设备数据上传获取参数:{}",shangHeUploadRequest);
        List<ShangHeUploadRequest.DataItem> dataList = shangHeUploadRequest.getDatas();
        if(CollectionUtils.isEmpty(dataList)){
            return;
        }
        ShangHeUploadRequest.DataItem dataItem = dataList.get(0);
        /**
         * 诊号
         */
        String visitNo = dataItem.getUserID();
        if(StringUtil.isBlank(visitNo)){
            log.info(">>>>>>>>>>上禾设备采集，诊号不能为空>>>>>>>>>>");
            return;
        }
        //身高
        BigDecimal height = BigDecimalUtils.toBigDecimal(dataItem.getHeight());
        if(BigDecimalUtils.eq(height,BigDecimal.ZERO)){
            log.info(">>>>>>>>>>上禾设备采集，身高数据错误>>>>>>>>>>");
            return;
        }
        //体重
        BigDecimal weight = BigDecimalUtils.toBigDecimal(dataItem.getWeight());
        if(BigDecimalUtils.eq(weight,BigDecimal.ZERO)){
            log.info(">>>>>>>>>>上禾设备采集，体重数据错误>>>>>>>>>>");
            return;
        }
        //1、查询挂号信息
        ClinicRegister clinicRegister = clinicRegisterService.getRegisterByVisitNo(visitNo);
        if(clinicRegister == null){
            log.info("上禾设备采集，没有根据诊号获取到挂号信息");
            return;
        }
        //2.更新挂号数据
        clinicRegister.setWeight(weight);
        clinicRegisterService.updateRegister(clinicRegister);
        //3.更新门诊数据
        ClinicMedical clinicMedical = clinicMedicalService.getMedicalByVisitNo(visitNo);
        if(clinicMedical != null){
            clinicMedical.setWeight(weight);
            clinicMedicalService.updateMedical(clinicMedical);
        }
        //4.更新患者信息
        ClinicPatient clinicPatient = new ClinicPatient();
        clinicPatient.setPatientId(clinicRegister.getPatientId());
        clinicPatient.setWeight(weight);
        clinicPatient.setHeight(BigDecimalUtils.formatInt(height));
        clinicPatientService.updatePatientInfo(clinicPatient);
    }
}
